package com.neusoft.neuiotms.dcam.fixed.converter;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;
import com.neusoft.neuiotms.dcam.common.repository.LocationRepository;
import com.neusoft.neuiotms.dcam.common.repository.ManufacturerRepository;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetExtFieldConfigDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.model.Maintenance;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.MaintenanceRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ProductRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetExtFieldConfigService;
import com.neusoft.neuiotms.dcam.fixed.vo.AssetExportVO;
import com.neusoft.neuiotms.dcam.fixed.vo.AssetImportVO;

@Component
public class AssetConverter extends AbstractConverter<Asset, AssetDTO> {

    @Autowired
    private AssetRepository assetRepository;

    @Autowired
    private MaintenanceRepository maintenanceRepository;

    @Autowired
    private ContractConverter constractConverter;

    DecimalFormat numberFormat = new DecimalFormat("##,##0.00");

    @Autowired
    private LocationRepository locationRepository;

    @Autowired
    private AssetTypeRepository assetTypeRepository;

    @Autowired
    private ContractRepository contractRepository;

    @Autowired
    private ManufacturerRepository manufacturerRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private AssetExtFieldConfigService assetExtFieldConfigService;

    @Autowired
    private DictionaryRepository dictionaryRepository;

    @SuppressWarnings("deprecation")
    @Override
    public Asset toModel(final AssetDTO dto) {
        Asset model;
        if (dto.isNew()) {
            model = new Asset();
        } else {
            model = this.assetRepository.getOne(dto.getId());
        }
        if ((dto.getCode() != null) && !dto.getCode().isEmpty()) {
            model.setCode(dto.getCode());
        }
        //类型
        if (!StringUtils.isEmpty(dto.getAssetTypeCode())) {
            try {
                model.setType(this.assetTypeRepository.findByCode(dto.getAssetTypeCode()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, dto.getAssetTypeCode());
            }
        }
        model.setName(dto.getName());
        model.setBooks(dto.getBooks());
        model.setUsedate(dto.getUsedate());
        model.setGetdate(dto.getGetdate());
        model.setOwnvalue(dto.getOwnvalue());

        model.setDepreciationstatus(dto.getDepreciationstatus());
        model.setDepreciationmonth(dto.getDepreciationmonth());
        model.setDepreciationtotal(dto.getDepreciationtotal());
        model.setMonth(dto.getMonth());
        model.setValuetype(dto.getValuetype());
        model.setMoney(dto.getMoney());
        model.setFundsfinancial(dto.getFundsfinancial());
        model.setUnfundsfinancial(dto.getUnfundsfinancial());
        model.setEquity(dto.getEquity());
        model.setSource(dto.getSourceDesc());
        model.setUsetype(dto.getUsetype());
        model.setStatus(dto.getStatusDesc());
        model.setUseDept(dto.getUseDeptDesc());
        model.setManageDept(dto.getManageDeptDesc());
        model.setUserName(dto.getUserName());
        model.setQuantity(dto.getQuantity());

        model.setCreator(dto.getCreator());
        model.setBelongTo(dto.getBelongTo());
        model.setSubmmitTo(dto.getSubmmitTo());
        model.setSpecification(dto.getSpecification());
        model.setLocated(dto.getLocated());
        model.setOriginCode(dto.getOriginCode());
        model.setBrand(dto.getBrand());
        model.setIdleValue(dto.getIdleValue());
        model.setHandoverDate(dto.getHandoverDate());
        model.setBackup(dto.getBackup());
        model.setRemark(dto.getRemark());
        model.setPurpose(dto.getPurpose());
        model.setProduce(dto.getProduce());
        model.setManufacturerEndDate(dto.getManufacturerEndDate());
        // 默认文档齐全
        model.setIsDocumentAll("NO".equals(dto.getIsDocumentAll()));
        model.setBusinessSystem(dto.getBusinessSystem());
        model.setSequence(dto.getSequence());
        model.setContractNameAndCode(dto.getContractNameAndCode());
        // 默认未报废
        model.setIsScrapped("YES".equals(dto.getIsScrapped()));
        model.setOfficeAddress(dto.getOfficeAddress());
        model.setOfficeComputerRoomAddress(dto.getOfficeComputerRoomAddress());
        // 默认运行
        if (StringUtils.isEmpty(dto.getRunning())) {
            model.setRunning("YES");
        } else {
            model.setRunning(dto.getRunning());
        }

        model.setOrgId(dto.getOrgId());
        model.setUserId(dto.getUserId());
        model.setUserName(dto.getUserName());
        model.setUpdateTime(new Date());
        model.setMeasureUnit(dto.getMeasureUnit());
        model.setExt1(dto.getExt1());
        model.setExt2(dto.getExt2());
        model.setExt3(dto.getExt3());
        model.setExt4(dto.getExt4());
        model.setExt5(dto.getExt5());
        model.setSecondeDept(dto.getSecondDept());
        model.setThirdDept(dto.getThirdDept());

        boolean mainteanceFlag = false;
        if (((null == dto.getMaintenanceId()) && (dto.getMaintenanceEnd() != null))
                || (dto.getMaintenanceStart() != null)
                || !StringUtils.isEmpty(dto.getMaintenanceUserId()) || !StringUtils
                        .isEmpty(dto.getMaintenanceNote())
                || !StringUtils.isEmpty(dto.getMaintenanceUserName())) {
            mainteanceFlag = true;
        }
        final Maintenance maintenanceModel = null != dto.getMaintenanceId()
                ? this.maintenanceRepository.getOne(dto.getMaintenanceId()) : mainteanceFlag ? new Maintenance() : null;
        if (maintenanceModel != null) {
            maintenanceModel.setMaintenanceEnd(dto.getMaintenanceEnd());
            maintenanceModel.setMaintenanceStart(dto.getMaintenanceStart());
            maintenanceModel.setMaintenanceUserId(dto.getMaintenanceUserId());
            maintenanceModel.setMaintenanceUserName(dto.getMaintenanceUserName());
            maintenanceModel.setMaintenanceNote(dto.getMaintenanceNote());
            model.setMaintenance(maintenanceModel);
        }
        //位置
        if (null != dto.getLocationId()) {
            try {
                model.setLocation(this.locationRepository.getOne(dto.getLocationId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, dto.getLocationId());
            }
        }
        if (!StringUtils.isEmpty(dto.getLocationCode())) {
            try {
                model.setLocation(this.locationRepository.findByCode(dto.getLocationCode()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, dto.getLocationId());
            }
        }
        //合同
        if (!StringUtils.isEmpty(dto.getContractCode())) {
            try {
                model.setContract(this.contractRepository.findByCode(dto.getContractCode()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, dto.getContractCode());
            }
        }
        //供应商
        if (null != dto.getManufacturerId()) {
            try {
                model.setManufacturer(this.manufacturerRepository.getOne(dto.getManufacturerId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, dto.getManufacturerId());
            }
        }
        //产品
        if (null != dto.getProductId()) {
            try {
                model.setProduct(this.productRepository.getOne(dto.getProductId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, dto.getProductId());
            }
        }
        return model;
    }

    @SuppressWarnings("deprecation")
    @Override
    public AssetDTO toDTO(final Asset model, final boolean forListView) {
        final AssetDTO dto = new AssetDTO();
        dto.setId(model.getId());
        dto.setCreateTime(model.getCreateTime());
        dto.setCode(model.getCode());
        if (model.getType() != null) {
            dto.setAssetTypeId(model.getType().getId());
            dto.setAssetTypeCode(model.getType().getCode());
            dto.setAssetTypeName(model.getType().getName());
            if (model.getType().getParent() != null) {
                dto.setCategory(model.getType().getParent().getName());
            }
        }
        dto.setName(model.getName());
        dto.setBooks(model.getBooks());

        dto.setUserId(model.getUserId());
        dto.setUserName(model.getUserName());
        dto.setOrgId(model.getOrgId());
        dto.setUsedate(model.getUsedate());
        dto.setGetdate(model.getGetdate());
        dto.setOwnvalue(model.getOwnvalue());
        dto.setDepreciationstatus(model.getDepreciationstatus());
        dto.setDepreciationmonth(model.getDepreciationmonth());
        dto.setDepreciationtotal(model.getDepreciationtotal());
        dto.setMonth(model.getMonth());
        dto.setValuetype(model.getValuetype());
        dto.setMoney(model.getMoney());
        dto.setFundsfinancial(model.getFundsfinancial());
        dto.setUnfundsfinancial(model.getUnfundsfinancial());
        dto.setEquity(model.getEquity());
        dto.setUsetype(model.getUsetype());
        dto.setStatusDesc(model.getStatus());
        dto.setSourceDesc(model.getSource());
        dto.setUseDeptDesc(model.getUseDept());
        dto.setManageDeptDesc(model.getManageDept());
        dto.setUserName(model.getUserName());
        dto.setQuantity(model.getQuantity());
        dto.setCreator(model.getCreator());
        dto.setBelongTo(model.getBelongTo());
        dto.setSubmmitTo(model.getSubmmitTo());
        dto.setSpecification(model.getSpecification());
        dto.setLocated(model.getLocated());
        dto.setOriginCode(model.getOriginCode());
        dto.setBrand(model.getBrand());
        dto.setIdleValue(model.getIdleValue());
        dto.setHandoverDate(model.getHandoverDate());
        dto.setBackup(model.getBackup());
        dto.setRemark(model.getRemark());
        dto.setPurpose(model.getPurpose());
        dto.setProduce(model.getProduce());
        dto.setSecondDept(model.getSecondeDept());
        dto.setThirdDept(model.getThirdDept());

        dto.setManufacturerEndDate(model.getManufacturerEndDate());
        if (model.getIsDocumentAll() != null) {
            dto.setIsDocumentAll(model.getIsDocumentAll().booleanValue() == true ? "是" : "否");
        } else {
            dto.setIsDocumentAll(StringUtils.EMPTY);
        }
        dto.setBusinessSystem(model.getBusinessSystem());
        if (model.getIsScrapped() != null) {
            dto.setIsScrapped(model.getIsScrapped().booleanValue() == true ? "是" : "否");
        } else {
            dto.setIsScrapped(StringUtils.EMPTY);
        }

        if (StringUtils.isNotEmpty(model.getRunning())) {
            if ("YES".equals(model.getRunning())) {
                dto.setRunning("是");
            } else if ("NO".equals(model.getRunning())) {
                dto.setRunning("否");
            } else {
                dto.setRunning(StringUtils.EMPTY);
            }
        }
        dto.setContractNameAndCode(model.getContractNameAndCode());
        dto.setOfficeAddress(model.getOfficeAddress());
        dto.setOfficeComputerRoomAddress(model.getOfficeComputerRoomAddress());
        dto.setSequence(model.getSequence());

        //        dto.setStatusDesc(model.getStatus() == null ? null : model.getStatus().getDesc());

        dto.setUpdateTime(model.getUpdateTime());
        dto.setMeasureUnit(model.getMeasureUnit());

        //        dto.setSourceDesc(model.getSource().getDesc());

        dto.setExt1(model.getExt1());
        dto.setExt2(model.getExt2());
        dto.setExt3(model.getExt3());
        dto.setExt4(model.getExt4());
        dto.setExt5(model.getExt5());
        dto.setExt1TsName(model.getExt1());
        dto.setExt2TsName(model.getExt2());
        dto.setExt3TsName(model.getExt3());
        dto.setExt4TsName(model.getExt4());
        dto.setExt5TsName(model.getExt5());

        final List<AssetExtFieldConfigDTO> extFields = this.assetExtFieldConfigService.findEffective();
        if (CollectionUtils.isNotEmpty(extFields)) {
            final Map<String, AssetExtFieldConfigDTO> mapExtFields =
                    extFields.stream()
                            .collect(Collectors.toMap(AssetExtFieldConfigDTO::getExtColumn, item -> item));

            this.setExtColumnShowName(model.getExt1(), dto, mapExtFields.get("ext1"));
            this.setExtColumnShowName(model.getExt2(), dto, mapExtFields.get("ext2"));
            this.setExtColumnShowName(model.getExt3(), dto, mapExtFields.get("ext3"));
            this.setExtColumnShowName(model.getExt4(), dto, mapExtFields.get("ext4"));
            this.setExtColumnShowName(model.getExt5(), dto, mapExtFields.get("ext5"));
        }

        if (model.getLocation() != null) {
            dto.setLocationId(model.getLocation().getId());
            dto.setLocationName(model.getLocation().getName());
            dto.setLocationCode(model.getLocation().getCode());
        }
        if (model.getContract() != null) {//合同
            dto.setContractCode(model.getContract().getCode());
            dto.setContractName(model.getContract().getName());
        }
        //产品
        if (model.getProduct() != null) {
            dto.setProductId(model.getProduct().getId());
            dto.setProductName(model.getProduct().getName());
        }

        if (model.getManufacturer() != null) {//厂商
            dto.setManufacturerId(model.getManufacturer().getId());
            dto.setManufacturerName(model.getManufacturer().getName());
            dto.setManufacturerPerson(model.getManufacturer().getPerson());
            dto.setManufacturerPhone(model.getManufacturer().getPhone());
        }

        if (model.getMaintenance() != null) {//维保
            dto.setMaintenanceId(model.getMaintenance().getId());
            dto.setMaintenanceEnd(model.getMaintenance().getMaintenanceEnd());
            dto.setMaintenanceStart(model.getMaintenance().getMaintenanceStart());
            dto.setMaintenanceUserId(model.getMaintenance().getMaintenanceUserId());
            dto.setMaintenanceUserName(model.getMaintenance().getMaintenanceUserName());
            dto.setMaintenanceNote(model.getMaintenance().getMaintenanceNote());
        }
        if (!forListView) {
            //            dto.setAssetType(this.assetTypeConverter.toDTO(model.getType()));
            //            if (model.getLocation() != null) {
            //                dto.setLocation(this.locationConverter.toDTO(model.getLocation()));
            //            }
            if (model.getContract() != null) {//合同
                dto.setContract(this.constractConverter.toDTO(model.getContract()));
            }
        }

        return dto;
    }

    private void setExtColumnShowName(final String extInfo, final AssetDTO dto,
            final AssetExtFieldConfigDTO extDto) {
        if (extDto != null) {
            if (extDto.getDataType().name().equals("multipledropdown")) {
                final List<String> list = JSON.parseArray(extInfo, String.class);
                if (CollectionUtils.isNotEmpty(list)) {
                    final List<Dictionary> dictionarys = this.dictionaryRepository.findByCodeIn(list);
                    if (CollectionUtils.isNotEmpty(dictionarys)) {
                        final List<String> strList =
                                dictionarys.stream().map(Dictionary::getText).collect(Collectors.toList());
                        if (extDto.getSequence() == 1) {
                            dto.setExt1TsName(Joiner.on(",").join(strList));
                        }
                        if (extDto.getSequence() == 2) {
                            dto.setExt2TsName(Joiner.on(",").join(strList));
                        }
                        if (extDto.getSequence() == 3) {
                            dto.setExt3TsName(Joiner.on(",").join(strList));
                        }
                        if (extDto.getSequence() == 4) {
                            dto.setExt4TsName(Joiner.on(",").join(strList));
                        }
                        if (extDto.getSequence() == 5) {
                            dto.setExt5TsName(Joiner.on(",").join(strList));
                        }
                    }
                }
            }
            if (extDto.getDataType().name().equals("droplist")) {
                final Dictionary dictionary = this.dictionaryRepository.findByCode(extInfo);
                if (dictionary != null) {
                    if (extDto.getSequence() == 1) {
                        dto.setExt1TsName(dictionary.getText());
                    }
                    if (extDto.getSequence() == 2) {
                        dto.setExt2TsName(dictionary.getText());
                    }
                    if (extDto.getSequence() == 3) {
                        dto.setExt3TsName(dictionary.getText());
                    }
                    if (extDto.getSequence() == 4) {
                        dto.setExt4TsName(dictionary.getText());
                    }
                    if (extDto.getSequence() == 5) {
                        dto.setExt5TsName(dictionary.getText());
                    }
                }
            }

        }
    }

    // todo hainan
    public AssetExportVO toVO(final Asset model) {
        final AssetExportVO dto = new AssetExportVO();
        dto.setId(model.getId());
        dto.setCreatetime(model.getCreateTime());
        // 资产大类
        dto.setAssetcategories(model.getType().getParent().getName());
        // 资产小类
        dto.setAssetTypeName(model.getType().getName());
        dto.setCode(model.getCode());
        dto.setName(model.getName());

        dto.setBooks(model.getBooks());
        dto.setUsedate(model.getUsedate());
        dto.setGetdate(model.getGetdate());
        if (model.getOwnvalue() != null) {
            dto.setOwnvalue(this.numberFormat.format(model.getOwnvalue()));
        }
        dto.setDepreciationmonth(model.getDepreciationmonth());
        dto.setDepreciationstatus(model.getDepreciationstatus());
        dto.setMonth(model.getMonth());
        if (model.getDepreciationtotal() != null) {
            dto.setDepreciationtotal(this.numberFormat.format(model.getDepreciationtotal()));
        }
        if (model.getMoney() != null) {
            dto.setMoney(this.numberFormat.format(model.getMoney()));
        }
        if (model.getUnfundsfinancial() != null) {
            dto.setUnfundsfinancial(this.numberFormat.format(model.getUnfundsfinancial()));
        }
        if (model.getFundsfinancial() != null) {
            dto.setFundsfinancial(this.numberFormat.format(model.getFundsfinancial()));
        }
        if (model.getEquity() != null) {
            dto.setEquity(this.numberFormat.format(model.getEquity()));
        }
        if (StringUtils.isNotEmpty(model.getValuetype())) {
            final Dictionary dictionary = this.dictionaryRepository.findByCode(model.getValuetype().trim());
            if (dictionary != null) {
                dto.setValuetype(dictionary.getText());
            }
        }
        dto.setUsetype(model.getUsetype());
        dto.setStatusDesc(model.getStatus());
        dto.setSourceDesc(model.getSource());
        dto.setUseDeptDesc(model.getUseDept());
        dto.setManageDeptDesc(model.getManageDept());
        dto.setUserName(model.getUserName());
        dto.setMonth(model.getMonth());

        dto.setQuantity(model.getQuantity());
        dto.setCreator(model.getCreator());
        dto.setBelongTo(model.getBelongTo());
        dto.setSpecification(model.getSpecification());
        dto.setSubmmitTo(model.getSubmmitTo());
        dto.setLocated(model.getLocated());
        dto.setOriginCode(model.getOriginCode());
        dto.setBrand(model.getBrand());
        if (model.getIdleValue() != null) {
            dto.setIdleValue(this.numberFormat.format(model.getIdleValue()));
        }
        dto.setHandoverDate(model.getHandoverDate());
        dto.setBackup(model.getBackup());
        dto.setRemark(model.getRemark());
        dto.setPurpose(model.getPurpose());
        dto.setProduce(model.getProduce());
        dto.setManufacturerEndDate(model.getManufacturerEndDate());
        if (model.getIsDocumentAll() != null) {
            dto.setIsDocumentAll(model.getIsDocumentAll() ? "是" : "否");
        }

        dto.setBusinessSystem(model.getBusinessSystem());
        dto.setContractNameAndCode(model.getContractNameAndCode());
        dto.setSequence(model.getSequence());
        if (model.getIsScrapped() != null) {
            dto.setIsScrapped(model.getIsScrapped() ? "是" : "否");
        }
        if (StringUtils.isNotEmpty(model.getRunning())) {
            dto.setRunning("YES".equals(model.getRunning()) ? "是" : "否");
        }
        dto.setOfficeAddress(model.getOfficeAddress());
        dto.setOfficeComputerRoomAddress(model.getOfficeComputerRoomAddress());
        //dto.setUser(model.getUserName());

        // dto(model.getCreateTime());
        // dto.setUpdateTime(model.getUpdateTime());
        //dto.setMeasureUnit(model.getMeasureUnit());
        //位置
        //dto.setLocationName(model.getLocation().getName());
        //产品
        //dto.setProduct(model.getProduct().getName());
        //合同
        //dto.setContract(model.getContract().getCode());
        // dto.setContractName(model.getContract().getName());
        //维保
        //            dto.setMaintenanceStart(model.getMaintenance().getMaintenanceStart());
        //            dto.setMaintenanceEnd(model.getMaintenance().getMaintenanceEnd());
        //            dto.setMaintenanceUserName(model.getMaintenance().getMaintenanceUserName());
        //            dto.setMaintenanceNote(model.getMaintenance().getMaintenanceNote());

        //供应商
        //            dto.setManufacturerName(model.getManufacturer().getName());
        //            dto.setManufacturerPerson(model.getManufacturer().getPerson());
        //            dto.setManufacturerPhone(model.getManufacturer().getPhone());
        //扩展属性
        //dto.setExt1(model.getExt1());
        //dto.setExt2(model.getExt2());
        //dto.setExt3(model.getExt3());
        //dto.setExt4(model.getExt4());
        //dto.setExt5(model.getExt5());
        return dto;
    }

    // todo vo to model
    public Asset toModel(final AssetImportVO dto) throws Exception {
        Asset model;
        if ((dto.getId() != null) && (dto.getId() > 0L)) {
            model = this.assetRepository.getOne(dto.getId());
        } else {
            model = new Asset();
        }
        model.setName(dto.getName());
        model.setCode(dto.getCode());
        model.setBooks(dto.getBooks());
        model.setUsedate(dto.getUsedate());
        model.setGetdate(dto.getGetdate());
        if (StringUtils.isNotEmpty(dto.getOwnvalue())) {
            model.setOwnvalue(this.numberFormat.parse(dto.getOwnvalue()).doubleValue());
        }
        model.setDepreciationmonth(dto.getDepreciationmonth());
        model.setDepreciationstatus(dto.getDepreciationstatus());
        model.setMonth(dto.getMonth());
        if (StringUtils.isNotEmpty(dto.getDepreciationtotal())) {
            model.setDepreciationtotal(this.numberFormat.parse(dto.getDepreciationtotal()).doubleValue());
        }
        if (StringUtils.isNotEmpty(dto.getMoney())) {
            model.setMoney(this.numberFormat.parse(dto.getMoney()).doubleValue());
        }
        if (StringUtils.isNotEmpty(dto.getUnfundsfinancial())) {
            model.setUnfundsfinancial(this.numberFormat.parse(dto.getUnfundsfinancial()).doubleValue());
        }
        if (StringUtils.isNotEmpty(dto.getFundsfinancial())) {
            model.setFundsfinancial(this.numberFormat.parse(dto.getFundsfinancial()).doubleValue());
        }
        if (StringUtils.isNotEmpty(dto.getEquity())) {
            model.setEquity(this.numberFormat.parse(dto.getEquity()).doubleValue());
        }
        model.setQuantity(dto.getQuantity());
        model.setCreator(dto.getCreator());
        model.setBelongTo(dto.getBelongTo());
        model.setSpecification(dto.getSpecification());
        model.setSubmmitTo(dto.getSubmmitto());
        model.setLocated(dto.getLocated());
        model.setOriginCode(dto.getOriginCode());
        model.setBrand(dto.getBrand());
        if (StringUtils.isNotEmpty(dto.getIdleValue())) {
            model.setIdleValue(this.numberFormat.parse(dto.getIdleValue()).doubleValue());
        }
        model.setHandoverDate(dto.getHandoverDate());
        model.setBackup(dto.getBackup());
        model.setRemark(dto.getRemark());
        model.setPurpose(dto.getPurpose());
        model.setProduce(dto.getProduce());
        model.setManufacturerEndDate(dto.getManufacturerEndDate());
        model.setBusinessSystem(dto.getBusinessSystem());
        model.setContractNameAndCode(dto.getContractNameAndCode());
        model.setSequence(dto.getSequence());
        model.setOfficeAddress(dto.getOfficeAddress());
        model.setOfficeComputerRoomAddress(dto.getOfficeComputerRoomAddress());
        model.setUsetype(dto.getUsetype());
        model.setUserName(dto.getUserName());
        if (StringUtils.isNotEmpty(dto.getIsDocumentAll())) {
            if ("是".equals(dto.getIsDocumentAll().trim())) {
                model.setIsDocumentAll(true);
            } else if ("否".equals(dto.getIsDocumentAll().trim())) {
                model.setIsDocumentAll(false);
            } else {
                model.setIsDocumentAll(true);
            }
        }
        if (StringUtils.isNotEmpty(dto.getRunning())) {
            if ("是".equals(dto.getRunning().trim())) {
                model.setRunning("YES");
            } else if ("否".equals(dto.getRunning())) {
                model.setRunning("NO");
            } else {
                model.setRunning("YES");
            }
        }
        if (StringUtils.isNotEmpty(dto.getIsScrapped())) {
            if ("是".equals(dto.getIsScrapped().trim())) {
                model.setIsScrapped(true);
            } else if ("否".equals(dto.getIsScrapped().trim())) {
                model.setIsScrapped(false);
            } else {
                model.setIsScrapped(false);
            }
        }

        if (!StringUtils.isEmpty(dto.getSourceDesc())) {
            model.setSource(dto.getSourceDesc());
        } else {
            model.setSource("purchase");
        }
        if (!StringUtils.isEmpty(dto.getStatusDesc())) {
            model.setStatus(dto.getStatusDesc());
        } else {
            model.setStatus("idle");
        }
        if (!StringUtils.isEmpty(dto.getUseDeptDesc())) {
            model.setUseDept(dto.getUseDeptDesc());
        } else {
            model.setUseDept(StringUtils.EMPTY);
        }
        if (!StringUtils.isEmpty(dto.getManageDeptDesc())) {
            model.setManageDept(dto.getManageDeptDesc());
        } else {
            model.setManageDept(StringUtils.EMPTY);
        }

        model.setUpdateTime(new Date());
        //model.setMeasureUnit(dto.getMeasureUnit());
        model.setMonth(dto.getMonth());
        model.setSequence(dto.getSequence());

        //扩展属性
        //model.setExt1(dto.getExt1());
        //model.setExt2(dto.getExt2());
        //model.setExt3(dto.getExt3());
        //model.setExt4(dto.getExt4());
        //model.setExt5(dto.getExt5());
        return model;
    }
}
